//
//  AllenLangdinPutView.m
//  自做商城项目
//
//  Created by allenL on 2017/6/23.
//  Copyright © 2017年 tam. All rights reserved.
//

#import "AllenLangdinPutView.h"

@interface AllenLangdinPutView()<UITextFieldDelegate>
@property (strong, nonatomic) UILabel *tostLable;  /** 提示label*/
@property (strong, nonatomic) UITextField *userNameText;  /** 手机号码*/
@property (strong, nonatomic) UITextField *passwordText;  /** 密码*/
@property (strong, nonatomic) UIButton *nextBtn;  /** 下一步button*/
@property (strong, nonatomic) UIButton *goLoginBtn;  /** 去登录*/
@property (strong, nonatomic) UILabel *textBackLable;  /** 输入框背景图*/
@property (strong, nonatomic) UILabel *textLineLable;  /** text的中间分隔线*/

@end

@implementation AllenLangdinPutView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {

        [self addSubview:self.tostLable];
        [self addSubview:self.textBackLable];
        [self addSubview:self.userNameText];
        [self addSubview:self.passwordText];
        [self addSubview:self.textLineLable];
        [self addSubview:self.nextBtn];
        [self addSubview:self.goLoginBtn];

        
        
    }
    return self;

}
-(void)layoutSubviews{
    [super layoutSubviews];
    __weak typeof (self) weakSelf = self;
    [_tostLable mas_makeConstraints:^(MASConstraintMaker *make){
        make.height.equalTo(@35);
        make.top.right.equalTo(weakSelf);
        make.left.equalTo(weakSelf.mas_left).offset(15);
    }];
    [_textBackLable mas_makeConstraints:^(MASConstraintMaker *make){
        make.height.equalTo(@89);
        make.top.equalTo(weakSelf.tostLable.mas_bottom);
        make.left.equalTo(weakSelf.mas_left).offset(-1);
        make.right.equalTo(weakSelf.mas_right).offset(1);
    }];
    
    [_userNameText mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.right.equalTo(weakSelf).offset(15);
        make.height.equalTo(@44);
        make.top.equalTo(weakSelf.textBackLable.mas_top);
    }];
    [_passwordText mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.right.equalTo(weakSelf).offset(15);
        make.height.equalTo(@44);
        make.top.equalTo(weakSelf.userNameText.mas_bottom).offset(1);
    }];
    [_textLineLable mas_makeConstraints:^(MASConstraintMaker *make){
        make.height.equalTo(@1);
        make.left.equalTo(weakSelf.textBackLable.mas_left).offset(15);
        make.right.equalTo(weakSelf.textBackLable.mas_right).offset(-15);
        make.centerY.equalTo(weakSelf.textBackLable.mas_centerY);
    }];
    
    [_nextBtn mas_makeConstraints:^(MASConstraintMaker *make){
        make.left.equalTo(weakSelf.mas_left).offset(16);
        make.right.equalTo(weakSelf.mas_right).offset(-16);
        make.height.equalTo(@35);
        make.top.equalTo(weakSelf.passwordText.mas_bottom).offset(15);
    }];
    [_goLoginBtn mas_makeConstraints:^(MASConstraintMaker *make){
        make.size.mas_equalTo(CGSizeMake(70, 16));
        make.right.equalTo(weakSelf.mas_right).offset(-15);
        make.top.equalTo(weakSelf.nextBtn.mas_bottom).offset(23);
        
    }];


}

- (UILabel *)tostLable{
    if (!_tostLable) {
        _tostLable = [[UILabel alloc]init];
        _tostLable.text = @"请输入手机号码注册新用户";
        _tostLable.font = [UIFont systemFontOfSize:14.0];
        _tostLable.textColor = RGB(81, 81, 81);
    }
    return _tostLable;
}
- (UILabel *)textBackLable{
    if (!_textBackLable) {
        _textBackLable = [[UILabel alloc]init];
        _textBackLable.backgroundColor = [UIColor whiteColor];
        _textBackLable.layer.borderWidth = 1;
        _textBackLable.layer.borderColor = [UIColor colorWithRed:188/255.0 green:188/255.0 blue:188/255.0 alpha:1].CGColor;
    }
    return _textBackLable;
    
}



-(UITextField *)userNameText{
    if (!_userNameText) {
        _userNameText = [[UITextField alloc]init];
        _userNameText.delegate = self;
        _userNameText.placeholder = @"请输入手机号码";
        [_userNameText addTarget:self action:@selector(userNameTextChangeText:) forControlEvents:
         (UIControlEventEditingChanged)];
    }
    return _userNameText;
}



-(UITextField *)passwordText{
    if (!_passwordText) {
        _passwordText = [[UITextField alloc]init];
        _passwordText.delegate = self;
        _passwordText.placeholder = @"设置账号的密码";
        [_passwordText addTarget:self action:@selector(passwordTextChangeText:) forControlEvents:
         (UIControlEventEditingChanged)];
        
    }
    return _passwordText;
}

-(UIButton *)nextBtn{
    if (!_nextBtn) {
        _nextBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        _nextBtn.backgroundColor = RGB(229, 229, 229);
        _nextBtn.userInteractionEnabled = NO;
        [_nextBtn setTitleColor:[UIColor colorWithRed:56/255.0 green:145/255.0 blue:241/255.0 alpha:1] forState:(UIControlStateNormal)];
        [_nextBtn addTarget:self action:@selector(pushNextViewController) forControlEvents:
         (UIControlEventTouchUpInside)];
        
    }
    return _nextBtn;
}

-(UIButton *)goLoginBtn{
    if (!_goLoginBtn) {
        _goLoginBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_goLoginBtn setTitle:@"去登录" forState:(UIControlStateNormal)];
        [_goLoginBtn setTitleColor:[UIColor colorWithRed:56/255.0 green:145/255.0 blue:241/255.0 alpha:1] forState:(UIControlStateNormal)];
    }
    return _goLoginBtn;
    
}
-(UILabel *)textLineLable{
    if (!_textLineLable) {
        _textLineLable = [[UILabel alloc]init];
        _textLineLable.backgroundColor = RGB(188, 188, 188);
    }
    return _textLineLable;
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == self.userNameText && range.location > 10) {
        return NO;
    }
    if (textField == self.passwordText && range.location > 4) {
        return NO;
    }
    
    return YES;
}

#pragma mark -textChange
//用户名修改内容时候调用的方法
- (void)userNameTextChangeText:(UITextField *)textfield{
    AllenLog(@"textfield.text = %@",textfield.text);
    if (textfield.text.length == 11 && _passwordText.text.length > 6) {
        _nextBtn.backgroundColor = RGB(56, 166, 241);
        _nextBtn.userInteractionEnabled = YES;
    }else{
        _nextBtn.backgroundColor = RGB(229, 229, 229);
        _nextBtn.userInteractionEnabled = NO;
    
    }
    
}
//密码修改内容时候调用的方法
- (void)passwordTextChangeText:(UITextField *)textfield{
    AllenLog(@"textfield.text = %@",textfield.text);
    if (textfield.text.length == 5 && _userNameText.text.length == 11) {
        _nextBtn.backgroundColor = RGB(56, 166, 241);
        _nextBtn.userInteractionEnabled = YES;
    }else{
        _nextBtn.backgroundColor = RGB(229, 229, 229);
        _nextBtn.userInteractionEnabled = NO;
        
    }
}

-(void) pushNextViewController{
    if (_nextBlock) {
        _nextBlock(@{@"userName":_userNameText.text,@"password":_passwordText.text});
    }
}
@end
